package cn.wolfcode.transfer.service.impl;

import cn.wolfcode.transfer.domain.Employee;
import cn.wolfcode.transfer.domain.LoginNotice;
import cn.wolfcode.transfer.domain.Notice;
import cn.wolfcode.transfer.domain.NoticeText;
import cn.wolfcode.transfer.mapper.EmployeeMapper;
import cn.wolfcode.transfer.mapper.NoticeMapper;
import cn.wolfcode.transfer.mapper.NoticeTextMapper;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.INoticeService;
import cn.wolfcode.transfer.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Text;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
@Autowired
    private NoticeTextMapper noticeTextMapper;

@Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void save(Notice notice) {
        notice.setCreationTime(new Date());
        notice.setCreator(UserContext.getCurrentUser());
        noticeMapper.insert(notice);
    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice see(Long id) {
        Long id1 = UserContext.getCurrentUser().getId();
        noticeTextMapper.road(id,id1);
        return noticeMapper.selectByPrimaryKey(id);
    }
    @Override
    public Notice get(Long id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Notice> listAll(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Notice> notices = noticeMapper.selectAll(qo); //里面不需要自己写limit
        return new PageInfo<Notice>(notices);
    }

    @Override
    public PageInfo<Notice> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Notice> notices = noticeMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Notice>(notices);
    }

    @Override
    public void push(Long id1) {
        List<Employee> employees = employeeMapper.selectAll();

        for (Employee employee :employees) {
            if (employee.getId()==UserContext.getCurrentUser().getId()){
                continue;
            }
            noticeTextMapper.push(employee.getId(),id1);
        }
        noticeMapper.pushStatus(id1,new Date());
    }

    @Override
    public void releaseAgain(Long id) {
        List<Employee> employees = employeeMapper.selectAll();
        List<Long> noticeTexts = noticeTextMapper.selectById(id);
        for (Employee employee :employees) {
            if (employee.getId()==UserContext.getCurrentUser().getId()||noticeTexts.contains(employee.getId())){
                continue;
            }
            noticeTextMapper.push(employee.getId(),id);
        }
        noticeMapper.pushStatus(id);
    }

    @Override
    public void cancel(Long id) {
        noticeTextMapper.deleteById(id);
        noticeMapper.cancel(id);
    }

    @Override
    public LoginNotice loginByIdGetNotice(Long id) {
        return new LoginNotice(noticeTextMapper.loginByIdGetNotice(id));
    }


}
